package com.itheima.ydd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.entity.SysConfig;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.vo.ChildrenVo;
import com.itheima.ydd.vo.SysConfigVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
@Slf4j
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    /**
     * 获取系统配置信息
     *
     * @return
     */
    @Override
    public R getAll() {
        //封装数据，分析
        /**
         * 第一层 ：为一个map集合
         * 键                 values
         * 1. list           name (String)        data(list) sysConfig
         *
         * 2. allData       集合（list）
         *
         * 3. configForm     map 集合（sysConfig attr_key 和 attr_values 这两列属性）
         *
         * 4. configRules    根据 AttrKey字段的属性值site_name查找到 validator的值
         */
        //1.创建一个map集合
        Map<String, Object> map = new HashMap<>();

        //2.封装configRules
        LambdaQueryWrapper<SysConfig> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SysConfig::getAttrKey, "site_name");
        SysConfig sysConfig = this.getOne(wrapper1);
        String validator = sysConfig.getValidator();
        Map<String, String> map1 = new HashMap<>();
        map1.put("site_name", validator);

        log.info("configRules" + map1.toString());

        //3.封装configForm
        LambdaQueryWrapper<SysConfig> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.select(SysConfig::getAttrKey, SysConfig::getAttrValue);
        List<SysConfig> sysConfigs = this.list(wrapper2);

        Map<String, String> map2 = new HashMap<>();
        for (SysConfig config : sysConfigs) {
            map2.put(config.getAttrKey(), config.getAttrValue());
        }
        log.info("configForm" + map2.toString());

        //4.封装allData --->sysConfigs1

        //List<SysConfig> sysConfigList = new ArrayList<>();
        LambdaQueryWrapper<SysConfig> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.select(SysConfig::getId, SysConfig::getAttrKey,SysConfig::getAttrValue,
                SysConfig::getValidator, SysConfig::getConfigValue, SysConfig::getType);
        List<SysConfig> sysConfigs1 = this.list(wrapper3);

        log.info("allData" + sysConfigs1.toString());



        //5.封装list属性
        SysConfigVo sysConfigVo = new SysConfigVo();

        //1.基础配置
        sysConfigVo.setName("基础配置");
        sysConfigVo.setKey("base");
        List<ChildrenVo> childrenList1 = new ArrayList<>();

        //1.1站点配置
        ChildrenVo children1 = new ChildrenVo();
        children1.setName("站点配置");
        LambdaQueryWrapper<SysConfig> wrapper4 = new LambdaQueryWrapper<>();
        wrapper4.eq(SysConfig::getGroupValue, "站点配置");
        List<SysConfig> list1 = this.list(wrapper4); //匹配到 groupValue 属性值等于 "站点配置"
        children1.setData(list1);
        log.info("list1" + list1.toString());

        //1.2底部信息
        ChildrenVo children2 = new ChildrenVo();
        children2.setName("底部信息");
        LambdaQueryWrapper<SysConfig> wrapper5 = new LambdaQueryWrapper<>();
        wrapper5.eq(SysConfig::getGroupValue, "底部信息");
        List<SysConfig> list2 = this.list(wrapper5); //匹配到 groupValue 属性值等于 "底部信息"
        children2.setData(list2);
        log.info("list2" + list2.toString());

        //1.3其他配置
        ChildrenVo children3 = new ChildrenVo();
        children3.setName("其他配置");
        LambdaQueryWrapper<SysConfig> wrapper6 = new LambdaQueryWrapper<>();
        wrapper6.eq(SysConfig::getGroupValue, "其他配置");
        List<SysConfig> list3 = this.list(wrapper6); //匹配到 groupValue 属性值等于 "其他配置"
        children3.setData(list3);
        log.info("list3" + list3.toString());

        //整合基础配置
        childrenList1.add(children1);
        childrenList1.add(children2);
        childrenList1.add(children3);
        //整合 children 属性
        sysConfigVo.setChildren(childrenList1);


        //2.系统配置
        SysConfigVo sysConfigVo1 = new SysConfigVo();

        sysConfigVo1.setName("系统设置");
        sysConfigVo1.setKey("system");
        List<ChildrenVo> childrenList2 = new ArrayList<>();

        //2.1"管理系统配置"
        ChildrenVo children4 = new ChildrenVo();
        children4.setName("管理系统配置");
        LambdaQueryWrapper<SysConfig> wrapper7 = new LambdaQueryWrapper<>();
        wrapper7.eq(SysConfig::getGroupValue, "管理系统配置");
        List<SysConfig> list4 = this.list(wrapper7); //匹配到 groupValue 属性值等于 "管理系统配置"
        children4.setData(list4);
        log.info("list4 *********************" + list4.toString());

        //整合系统设置
        childrenList2.add(children4);

        sysConfigVo1.setChildren(childrenList2);

        //3.其他设置
        SysConfigVo sysConfigVo2 =new SysConfigVo();

        sysConfigVo2.setName("其他设置");
        sysConfigVo2.setKey("custom");

        List<ChildrenVo> childrenList3 =new ArrayList<>();

        //3.1 通知公告
        ChildrenVo children5 =new ChildrenVo();
        children5.setName("通知公告");
        LambdaQueryWrapper<SysConfig> wrapper8 = new LambdaQueryWrapper<>();
        wrapper8.eq(SysConfig::getGroupValue, "通知公告");
        List<SysConfig> list5 = this.list(wrapper8); //匹配到 groupValue 属性值等于 "通知公告"
        children5.setData(list5);
        log.info("list5" + list5.toString());

        //3.2 前台图片
        ChildrenVo children6 =new ChildrenVo();
        children6.setName("前台图片");
        LambdaQueryWrapper<SysConfig> wrapper9 = new LambdaQueryWrapper<>();
        wrapper9.eq(SysConfig::getGroupValue, "前台图片");
        List<SysConfig> list6 = this.list(wrapper9); //匹配到 groupValue 属性值等于 "前台图片"
        children6.setData(list6);
        log.info("list6" + list6.toString());

        //整合其他设置
        childrenList3.add(children5);
        childrenList3.add(children6);

        sysConfigVo2.setChildren(childrenList3);

        List<SysConfigVo> sysConfigVos=new ArrayList<>();
        sysConfigVos.add(sysConfigVo);
        sysConfigVos.add(sysConfigVo1);
        sysConfigVos.add(sysConfigVo2);

        map.put("list",sysConfigVos);
        map.put("allData",sysConfigs1);
        map.put("configForm",map2);
        map.put("configRules",map1);

        R r= new R();
        r.setData(map);
        r.setErrmsg("");
        r.setErrno(0);
        return r;

    }
}
